<?php
// No directAcces
defined('_AppBackMiage') or die('Restricted access');

include_once (DS . "backoffice/include/header.php");

if (isset($_POST['submit']))
{
    $date = new Date();

    $form = true;
    if (isset($_POST['promo']) && !empty($_POST['promo']))
    {
        $tmp = $date->setIdPromo($_POST['promo']);
        if ($tmp['boolOk'] === false)
        {
            $errMSG = $tmp['errmsg'];
            $form = false;
        }
    }
    if (isset($_POST['titre']) && !empty($_POST['titre']))
    {
        $tmp = $date->setTitreDate($_POST['titre']);
        if ($tmp['boolOk'] === false)
        {
            $errMSG = $tmp['errmsg'];
            $form = false;
        }
    }
    if (isset($_POST['valeur']) && !empty($_POST['valeur']))
    {
        switch ($_POST['type'])
        {
            case 0:
                $tmp = $date->setValeurDate(Tools::transformDateToTimestamp($_POST['valeur']));
                $tmp = $date->setTypeDate(0);
                break;
            case 1:
                $mois = 0;
                $jours = 0;
                if( isset($_POST['valeur']) && !empty($_POST['valeur']))
                {
                    foreach($_POST['valeur'] as $info => $valeur)
                    {
                        if($info == "mois")
                        {
                            $mois = $valeur;
                        }
                        if( $info == "jours" )
                        {
                            $jours = $valeur;
                        }
                    }
                }
                $time = Tools::transformDureeToTimestamp($mois, $jours);
                $tmp = $date->setValeurDate($time);
                $tmp = $date->setTypeDate(1);
                break;
            case 2:
                $tmp = $date->setValeurDate($_POST['valeur']);
                $tmp = $date->setTypeDate(2);
                break;
        }
        if ($tmp['boolOk'] === false)
        {
            $errMSG = $tmp['errmsg'];
            $form = false;
        }
    }

    if ($form)
    {
        $date->insert();
    }
}

$promos = Promo::getListPromo();
?>
<div id="wrapper">
    <div id="content">
        <div id="box">
            <h3 id="adduser">Ajout d'une date</h3>
            <form id="form" method="post">
                <fieldset id="personal">
                    <legend>Informations</legend>
                    <label for="promo">Promo concern&eacute;e : </label>
                    <select name="promo">
                        <?php
                            foreach($promos as $id => $promo)
                            {
                                ?>

                                <option value="<?php echo $id; ?>"><?php echo $promo; ?> </option>

                                <?php
                            }
                        ?>
                    </select>
                    <br/>
                    <label for="titre">Titre : </label>
                    <input name="titre" id="titre" type="text" tabindex="1" />
                    <?php
                    if ($form === false)
                    {
                        echo $errMSG;
                    }
                    ?>
                    <br />
                    <label for="type">Type :</label>
                    <select name="type" onchange="changeInput(this);">
                        <option value="0">Date limite</option>
                        <option value="1">Dur&eacute;e</option>
                        <option value="2">Texte simple</option>
                    </select>
                    <?php
                    if ($form === false)
                    {
                        echo $errMSG;
                    }
                    ?>
                    <br/>
                    <label for="valeur">Valeur : </label>
                    <input class="datepicker" name="valeur" id="valeur" type="text" tabindex="1" />
                    <?php
                    if ($form === false)
                    {
                        echo $errMSG;
                    }
                    ?>
                    <br />
                </fieldset>
                <div align="center">
                    <input id="button1" type="submit" name="submit" value="Ajouter" />
                    <input id="button2" type="reset" />
                </div>
            </form>
            <script type="text/javascript">
                function changeInput(select)
                {
                    var inputValue = document.getElementById("valeur");
                    switch(select.options[select.selectedIndex].value)
                    {
                        case "0":
                            var newBalise = document.createElement("input");
                            newBalise.id = "valeur";
                            newBalise.type = "text";
                            newBalise.tabindex = "1";
                            newBalise.name="valeur";
                            document.getElementById("personal").replaceChild(newBalise, inputValue);
                            $("#valeur").datepicker({dateFormat : 'dd/mm/yy'});
                            $.datepicker.setDefaults($.datepicker.regional['fr']);
                            break;
                        case "1":
                            $("#valeur").datepicker("destroy");

                            var div = document.createElement("div");
                            div.id = "valeur";

                            var selectMois = document.createElement("input");
                            var textMois = document.createTextNode(" mois et ");
                            selectMois.id = "valeur[mois]";
                            selectMois.name = "valeur[mois]";
                            selectMois.type = "text";
                            selectMois.size = "1";
                            selectMois.value = "3";

                            div.appendChild(selectMois);
                            div.appendChild(textMois);

                            var selectJours = document.createElement("input");
                            var textJours = document.createTextNode(" jours.");
                            selectJours.id = "valeur[jours]";
                            selectJours.name = "valeur[jours]";
                            selectJours.type = "text";
                            selectJours.size = "2";
                            selectJours.value = "15";

                            div.appendChild(selectJours);
                            div.appendChild(textJours);

                            document.getElementById("personal").replaceChild(div, inputValue);

                            break;
                        case "2":
                            $("#valeur").datepicker("destroy");

                            var newBalise = document.createElement("input");
                            newBalise.id = "valeur";
                            newBalise.type = "text";
                            newBalise.tabindex = "1";
                            newBalise.name="valeur";

                            document.getElementById("personal").replaceChild(newBalise, inputValue);

                            break;
                    }
                }
            </script>
        </div>
    </div>
    <?php
                    include_once (DS . "backoffice/include/sidebar.php");
                    include_once (DS . "backoffice/include/footer.php");
    ?>

